package com.kt.emall.order.interceptor;

import com.kt.common.constant.AuthServerConstant;
import com.kt.common.vo.MemberRespVO;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @program: e-mall
 * @description: 用户登录拦截器
 * @author: KamTang
 * @create: 2022-02-10 09:00
 **/
@Component
public class UserLoginInterceptor implements HandlerInterceptor {

    public static ThreadLocal<MemberRespVO> loginUser = new ThreadLocal<>();

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        MemberRespVO attribute = (MemberRespVO) request.getSession().getAttribute(AuthServerConstant.LOGIN_USER);
        if (attribute != null) {
            loginUser.set(attribute);
            return true;
        } else {
            request.getSession().setAttribute("msg", "请先进行登录");
            response.sendRedirect("http://auth.gulumall.com/login.html");
            return false;
        }
    }
}
